//
//  SystemViewController.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "SystemViewController.h"
#import "Masonry.h"
#import "SDImageCache.h"
#import "FeedBackViewController.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "XMRegisterViewController.h"
#import "WKProgressHUD.h"
@interface SystemViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UIView *viewOne;
@property (nonatomic,strong) UIView *viewTwo;
@property (nonatomic,strong) UIView *viewThree;
@property (nonatomic,strong) UIImageView *backOutView;
@property (strong,nonatomic) NSString *cacheStr;
@property (strong,nonatomic) UILabel *cashLab;
@property (strong,nonatomic) UIButton *sysBtn;
@property (nonatomic) BOOL isOn;
@property (nonatomic,strong) WKProgressHUD *hud;
@end

@implementation SystemViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationItem.title=@"系统设置";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMyView) name:@"showMyView" object:nil];
   
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)showMyView{
    
    self.hud=[WKProgressHUD popMessage:@"意见反馈成功,感谢您的参与" inView:self.view duration:1 animated:YES];
}
-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    [self initUI];
}
-(void)initUI{
    
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    [self.view addSubview:self.backgroundViews];
    [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(kNavbarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight));
        
    }];
    
    self.viewOne = [[UIView alloc] init];
    self.viewOne.backgroundColor=[UIColor whiteColor];
    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOneBack:)];
    tapOne.delegate=self;
    [self.viewOne addGestureRecognizer:tapOne];
    [self.backgroundViews addSubview:self.viewOne];
    [self.viewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backgroundViews.mas_top).offset(10);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.height.equalTo(@(52));
        
    }];
    
    UILabel *labOne=[[UILabel alloc] init];
    labOne.text = @"意见反馈";
    labOne.font = [UIFont systemFontOfSize:15];
    [self.viewOne addSubview:labOne];
    [labOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewOne.mas_top).offset(15);
        make.left.equalTo(self.viewOne.mas_left).offset(15);
        make.right.equalTo(self.viewOne.mas_right).offset(-90);
        make.bottom.equalTo(self.viewOne.mas_bottom).offset(-15);
    }];
    
    UIImageView *rightV=[[UIImageView alloc] init];
    rightV.image = [UIImage imageNamed:@"icon_jt_you"];
    [self.viewOne addSubview:rightV];
    [rightV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewOne.mas_top).offset(20);
        make.right.equalTo(self.viewOne.mas_right).offset(-25);
    }];
    
    self.viewTwo = [[UIView alloc] init];
    self.viewTwo.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.viewTwo];
    [self.viewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewOne.mas_bottom).offset(10);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.height.equalTo(@(52));
        
    }];
    UILabel *labTwo=[[UILabel alloc] init];
    labTwo.text = @"系统通知";
    labTwo.font = [UIFont systemFontOfSize:15];
    [self.viewTwo addSubview:labTwo];
    [labTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewTwo.mas_top).offset(15);
        make.left.equalTo(self.viewTwo.mas_left).offset(15);
        make.right.equalTo(self.viewTwo.mas_right).offset(-130);
        make.bottom.equalTo(self.viewTwo.mas_bottom).offset(-15);
    }];
    //开关按钮
    self.sysBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]] isEqualToString:@"IsSelected"]) {
        [self.sysBtn setImage:[UIImage imageNamed:@"icon_tz_l"] forState:UIControlStateNormal];
        self.sysBtn.selected=YES;
    }else if ([[[NSUserDefaults standardUserDefaults] objectForKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]] isEqualToString:@"NoSelected"]){
        
        [self.sysBtn setImage:[UIImage imageNamed:@"icon_tz_h"] forState:UIControlStateNormal];
        self.sysBtn.selected=NO;
    }else{
        
        [self.sysBtn setImage:[UIImage imageNamed:@"icon_tz_h"] forState:UIControlStateNormal];
    }
    
    
    [self.sysBtn addTarget:self action:@selector(changePictues:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTwo addSubview:self.sysBtn];
    [self.sysBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewTwo.mas_top).offset(18);
        make.right.equalTo(self.viewTwo.mas_right).offset(-25);
    }];
    
    self.viewThree = [[UIView alloc] init];
    self.viewThree.userInteractionEnabled=YES;
    self.viewThree.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.viewThree];
    [self.viewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewTwo.mas_bottom).offset(1.3);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.height.equalTo(@(52));
        
    }];
    UILabel *labThree=[[UILabel alloc] init];
    labThree.text = @"清空缓存";
    labThree.font = [UIFont systemFontOfSize:15];
    UITapGestureRecognizer *tapthree = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearBeat:)];
    tapthree.delegate=self;
    [self.viewThree addGestureRecognizer:tapthree];
    [self.viewThree addSubview:labThree];
    [labThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewThree.mas_top).offset(15);
        make.left.equalTo(self.viewThree.mas_left).offset(15);
    }];
    
    UIImageView *rightVs=[[UIImageView alloc] init];
    rightVs.image = [UIImage imageNamed:@"icon_jt_you"];
    [self.viewThree addSubview:rightVs];
    [rightVs mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.viewThree.mas_top).offset(20);
        make.right.equalTo(self.viewThree.mas_right).offset(-25);
    }];
    
    self.cashLab = [[UILabel alloc] init];
    self.cacheStr = [self calculateCacheSize];
    self.cashLab.text = self.cacheStr;;
    self.cashLab.font =[UIFont systemFontOfSize:13];
    self.cashLab.textAlignment = NSTextAlignmentRight;
    [self.viewThree addSubview:self.cashLab];
    [self.cashLab mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.viewThree.mas_top).offset(15);
        make.bottom.equalTo(self.viewThree.mas_bottom).offset(-15);
        make.left.equalTo(labThree.mas_right).offset(5);
        make.right.equalTo(rightVs.mas_left).offset(-5);
        
    }];
    
    //退出登录按钮
    self.backOutView =[[UIImageView alloc] init];
    self.backOutView.image=[UIImage imageNamed:@"icon_anniu_f"];
    self.backOutView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backout:)];
    tap.delegate=self;
    [self.backOutView addGestureRecognizer:tap];
    [self.backgroundViews addSubview:self.backOutView];
    [self.backOutView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.viewThree.mas_bottom).offset(230);
        make.left.equalTo(self.backgroundViews.mas_left).offset(30);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-30);
      
        
    }];
    UILabel *backLab=[[UILabel alloc] init];
    backLab.text = @"退出登录";
    backLab.userInteractionEnabled =YES;
    backLab.textColor = [UIColor whiteColor];
    backLab.textAlignment = NSTextAlignmentCenter;
    [self.backOutView addSubview:backLab];
    [backLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backOutView.mas_top).offset(30);
        make.left.equalTo(self.backOutView.mas_left);
        make.right.equalTo(self.backOutView.mas_right);
        make.height.equalTo(@(20));
    }];
    
}
-(void)changePictues:(UIButton *)btn{
    NSString *userName=[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    self.sysBtn.selected =!self.sysBtn.selected;
    if (self.sysBtn.selected)
    {
        [self.sysBtn setImage:[UIImage imageNamed:@"icon_tz_l"] forState:UIControlStateNormal];
        self.isOn = YES;
        
        [[NSUserDefaults standardUserDefaults]setObject:@"IsSelected" forKey:userName];
        
    }else{

        [self.sysBtn setImage:[UIImage imageNamed:@"icon_tz_h"] forState:UIControlStateNormal];
        self.isOn = NO;
         [[NSUserDefaults standardUserDefaults]setObject:@"NoSelected" forKey:userName];
    }
}
//意见反馈的点击事件
-(void)tapOneBack:(UIGestureRecognizer *)tp{
   
    FeedBackViewController *vc = [[FeedBackViewController alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
 
    [self.navigationController pushViewController:vc animated:YES];
}
//清空缓存的点击事件
-(void)clearBeat:(UIGestureRecognizer *)tp{
    
    //缓存清理
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];

    self.cashLab.text = @"0";
}
//退出登录的点击事件
-(void)backout:(UIGestureRecognizer *)tp{
    
    
    [SVProgressHUD showWithStatus:@"正在退出"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
    NSDictionary *dd=@{@"":@""};
    [GPosAPI requestverifyLogOutWithParams:dd andCallback:^(id obj) {
        
        if (obj==nil)
        {
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:@"请求失败,请检查您的网络状态"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else{
        
        if ([[obj objectForKey:@"code"] integerValue] ==200)
        {
            [SVProgressHUD dismiss];
            
            UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            //storyboard里的ID
            XMRegisterViewController *VC = [mainStory instantiateInitialViewController];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
            VC.hidesBottomBarWhenPushed=YES;
            //设置当前窗口的根控制器
             [UIApplication sharedApplication].keyWindow.rootViewController=VC;
           
            
        }else if ([[obj objectForKey:@"code"] integerValue]==401 || [[obj objectForKey:@"code"] intValue] == 402){
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NewLoginInView" object:nil];
            UIStoryboard * mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            XMRegisterViewController *VC = [mainStory instantiateInitialViewController];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
            VC.hidesBottomBarWhenPushed=YES;
            //设置当前窗口的根控制器
            [UIApplication sharedApplication].keyWindow.rootViewController=VC;
            [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }
        else{
            
            [SVProgressHUD dismiss];
            [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }
        }
    }];
    
}
//计算缓存大小
-(NSString*)calculateCacheSize{
    
    NSInteger sizeKb = [[SDImageCache sharedImageCache] getSize] / 1024;
    
    //NSInteger --> float
    NSString* strr = [NSString stringWithFormat:@"%ld",sizeKb];
    float sizekb = [strr floatValue];
    
    float M = sizekb / 1024;
    
    if (M >= 1) {
        
        NSString* str = [NSString stringWithFormat:@"%.2f M",M];
        
        return str;
        
    }else{
        
        NSString* str = [NSString stringWithFormat:@"%ld kB",sizeKb];
        
        return str;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
